package com.hk.Mgr.web.controller.order;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hk.commons.StringHelper;
import com.hk.service.IUserChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hk.Mgr.web.message.sys.SysUserReq;
import com.hk.base.BaseCtrl;
import com.hk.base.ResponseBase;
import com.hk.commons.DateUtil;
import com.hk.commons.ResultCode;
import com.hk.commons.entity.view.OrderView;
import com.hk.commons.entity.view.UserView;
import com.hk.service.view.IOrderView;
import com.hk.service.view.IPaymentView;
import com.hk.service.view.IReservView;
import com.hk.service.view.IUserView;

/**
 * 后台获取佣金相关信息
 * 
 * @author jeff
 *
 */
@Controller
@RequestMapping(value = BaseCtrl.MgrRootPath + "/commCtrl")
public class CommissionCtrl {
	private Logger logger = LoggerFactory.getLogger(CommissionCtrl.class);

	@Autowired
	private IOrderView orderViewService;

	@Autowired
	private IUserView userViewService;

	@Autowired
	private IUserChannel userChannelService;

	@Autowired
	private IReservView reservViewService;

	@Autowired
	private JdbcTemplate jdbcTemplate;

	/**
	 * 我的佣金=产品购买金额*产品佣金比例*产品期限（天数）/365
	 */
	@ResponseBody
	@RequestMapping(value = "/getList", produces = "application/json;charset=UTF8", method = RequestMethod.POST)
	public ResponseBase<List<Map<String, Object>>> getList(
			@RequestBody SysUserReq userReq) {

		ResponseBase<List<Map<String, Object>>> resp = new ResponseBase<List<Map<String, Object>>>();

		List<Map<String, Object>> commissionList = new ArrayList<Map<String, Object>>();
		Long userId = userReq.getUserId();

		String userRealName = userReq.getName();

		String userOrgName = userReq.getUserOrgName();

		Integer curPage = userReq.getCurPage();

		Integer pageSize = userReq.getPageSize();

		logger.info(
				"CommissionCtrl getList userId = {},userRealName ={}, userOrgName = {}, curPage ={}, pageSize ={} ",
				userId, userRealName, userOrgName, curPage, pageSize);
		
		if(userId == null){
			resp.setResultCode(ResultCode.PARAM_LACK.getCode());
			resp.setMsg("登录超时，请重新登录"); 
		}
		
		if (pageSize > 50) {
			Long size = userViewService.getTotalCount(userId, userRealName,
					userOrgName);

			Map<String, Object> map = new HashMap<String, Object>();

			map.put("size", size);
			commissionList.add(map);
			resp.setResultCode(ResultCode.SUCC.getCode());
			resp.setResult(commissionList);
			resp.setSize(size);

		} else {

			List<UserView> userViewList = userViewService.getAllSubUser(userId,
					userRealName, userOrgName, curPage, pageSize);
			
			
			for (UserView userView : userViewList) {
				BigDecimal commiTotal = orderViewService.getTotalCommByUser(
						userView.getUserId(), null);

				BigDecimal commiPay = orderViewService.getTotalCommByUser(
						userView.getUserId(), 1);

				BigDecimal commiUnPay = orderViewService.getTotalCommByUser(
						userView.getUserId(), 0);

				Map<String, Object> map = new HashMap<String, Object>();

				map.put("userId", userView.getUserId());
				map.put("userRealName", userView.getUserRealName());
				map.put("userPhone", userView.getUserPhone());

				if(userView.getUserChannel() != null){
					Integer channel = userView.getUserChannel();
					map.put("userChannel", channel);

					String channelName = userChannelService.getChannelNameByChannelId(channel);
					if(StringHelper.isNotEmpty(channelName)){
						map.put("userChannelName", channelName);
					}else{
						map.put("userChannelName", "");
					}
				}

				map.put("userOrgId", userView.getUserOrgId());
				map.put("userOrgName", userView.getUserOrgName());
				map.put("commTotal", commiTotal);
				map.put("commiPay", commiPay);
				map.put("commiUnPay", commiUnPay);

				commissionList.add(map);
			}

			resp.setResultCode(ResultCode.SUCC.getCode());
			resp.setResult(commissionList);
			resp.setSize(commissionList.size());
		}
		return resp;
	}

	@ResponseBody
	@RequestMapping(value = "/getCommOrder", method = RequestMethod.POST)
	public ResponseBase<List<Map<String, Object>>> getCommOrder(
			@RequestBody SysUserReq userReq) {
		ResponseBase<List<Map<String, Object>>> resp = new ResponseBase<List<Map<String, Object>>>();

		List<Map<String, Object>> commOrderList = new ArrayList<Map<String, Object>>();

		Long userId = userReq.getUserId();

		String userRealName = userReq.getName();

		String finUserName = userReq.getFinUserName();

		String orderOrgName = userReq.getUserOrgName();

		Integer curPage = userReq.getCurPage();

		Integer pageSize = userReq.getPageSize();

		Integer reqType = userReq.getReqType();
		
		
		
		
		logger.info(
				"CommissionCtrl getList userId = {},userRealName ={}, finUserName={}, userOrgName = {}, curPage ={}, pageSize ={}, reqType={} ",
				userId, userRealName, finUserName, orderOrgName, curPage,
				pageSize, reqType);
		
		if(userId == null){
			resp.setResultCode(ResultCode.PARAM_LACK.getCode());
			resp.setMsg("登录超时，请重新登录"); 
		}
		
		if (pageSize > 50) {
			Long size = orderViewService.getSize(userId, userRealName,
					finUserName,orderOrgName, reqType);

			Map<String, Object> map = new HashMap<String, Object>();

			map.put("size", size);
			commOrderList.add(map);
			resp.setResultCode(ResultCode.SUCC.getCode());
			resp.setResult(commOrderList);
			resp.setSize(size);
		} else {
			List<OrderView> orderViewList = orderViewService.getAll(userId,
					userRealName, finUserName, orderOrgName, curPage, pageSize, reqType);

			Long size = orderViewService.getSize(userId, userRealName,
					finUserName, orderOrgName, reqType);

			for (OrderView orderView : orderViewList) {
				logger.info("getCommOrder orderId={}", orderView.getOrderId());
				Map<String, Object> map = new HashMap<String, Object>();

				map.put("orderId", orderView.getOrderId());
				map.put("userRealName", orderView.getUserRealName());
				map.put("userOrgId", orderView.getUserOrgId());
				map.put("userOrgName", orderView.getUserOrgName());
				map.put("prodName", orderView.getProductName());
				map.put("successAmount", orderView.getSuccessAmount());
				map.put("orderCreatedOn",
						DateUtil.longToLongDate(orderView.getOrderCreatedOn()));
				map.put("finUserName", orderView.getFinUserRealName());
				if(orderView.getOrderChannel() != null){
					Integer channel = orderView.getOrderChannel();
					map.put("orderChannel", channel);

					String channelName = userChannelService.getChannelNameByChannelId(channel);
					if(StringHelper.isNotEmpty(channelName)){
						map.put("orderChannelName", channelName);
					}
				}
				map.put("orderOrgName", orderView.getOrderOrgName());
				map.put("commissionAmount", orderView.getCommissionAmount());
				map.put("commissionRate", orderView.getCommissionRate());
				map.put("commissionStatus", orderView.getCommissionStatus());
				commOrderList.add(map);
			}

			resp.setResultCode(ResultCode.SUCC.getCode());
			resp.setResult(commOrderList);
			resp.setSize(size);
		}
		return resp;
	}

}